<?php

namespace app\adminbackstage\controller;

use app\adminbackstage\service\MerchantService;
use app\BaseController;
use app\Response;
use think\facade\Request;
use think\Validate;
use app\adminbackstage\validate\Address as AdminMerchantAddress;

class Merchant extends BaseController
{
    /***
     * 商家列表
    */
    public function merchantList(){
        if (Request::isPost()) {
            $page = Request::param('page');
            $num = Request::param('num');
            $phone = Request::param('phone');
            $wx_account = Request::param("wx_account");
            $nickname = Request::param("nickname");
            $username = Request::param("username");
            $res = MerchantService::merchantListService($page,$num,$phone,$wx_account,$nickname,$username);
            return $res;
        }
    }


    /***
     * 商家详情
     */
    public function merchantInfo(){
        if (Request::isPost()) {
            $m_id = Request::param('uid');
            $res = MerchantService::merchantInfoService($m_id);
            return $res;
        }
    }


    /***
     * 商家添加、修改
     */
    public function merchantAdd(){
        if (Request::isPost()) {
            $id = Request::param("uid");
            $username = Request::param('username');
            $password = Request::param('password','kd123456');
            $phone = Request::param('phone');
            $wx_account = Request::param('wx_account');
            $nickname = Request::param('nickname');
            $remarks = Request::param("remarks");
            $paid = Request::param("paid");
            $enable = Request::param("enable","");
            if (empty($phone)){
                return Response::wrapError(0,"请输入手机号");
            }
            $res = MerchantService::merchantAddService($username,$password,$paid,$phone,$wx_account,$nickname,$remarks,$enable,$id);
            return $res;
        }
    }

    /***
     * 商家地址列表
     */
    public function merchantAddressList(){
        if (Request::isPost()) {
            $m_id = Request::param('uid');
            $page = Request::param("page",1);
            $num = Request::param("num",20);
            $res = MerchantService::merchantAddListService($m_id,$page,$num);
            return $res;
        }
    }


    /*** 商家地址添加
    */
    public function merchantAddressCreat(){
        if (Request::isPost()) {
            $m_id = Request::param("uid");
            $id = Request::param("id","");
            $paid = Request::param("paid","");
            $phone = Request::param("phone","");
            $platform = Request::param("platform","");
            $shop = Request::param("shop","");
            $province = Request::param("province","");
            $city = Request::param("city","");
            $area = Request::param("area","");
            $street = Request::param("street","");
            $address = Request::param("address","");
            $default = Request::param("default",0);
            $significant = Request::param("significant",1);
            $enable = Request::param("enable",1);
            try {
                validate(AdminMerchantAddress::class)->check([
                    'phone' => $phone,
                    'platform' => $platform,
                    'shop' => $shop,
                    'province' => $province,
                    'city' => $city,
                    'area' => $area,
                    'address' => $address,
                    'default' => $default,
                    'significant' => $significant
                ]);
            } catch (\Exception $e) {
                // 验证失败 输出错误信息
                return Response::wrapError(0,lang($e->getMessage()));
            }
            $data = [
                'phone' => $phone,
                'platform' => $platform,
                'paid' => $paid,
                'shop' => $shop,
                'uid' => $m_id,
                'province' => $province,
                'city' => $city,
                'area' => $area,
                'address' => $address,
                'street' => $street,
                'default' => $default,
                'addtime' => time(),
                'significant' => $significant,
                'enable' => $enable
            ];
            if(empty($id)){
                $data["addtime"] = time();
            }
            $res = MerchantService::merchantAddCreatService($m_id,$id,$data);
            return Response::wrapData();
        }
    }
    /*** 商家地址详情
    */
    public function merchantAddressDetail(){
        if (Request::isPost()) {
            $id = Request::param("id");
            $res = MerchantService::merchantAddDetailService($id);
            return $res;
        }
    }

    public function merchantAddressDel(){
        if (Request::isPost()) {
            $id = Request::param("id");
            $res = MerchantService::merchantAddDeleteService($id);
            return $res;
        }
    }
}